package com.xu.sso.db.service.impl;

import com.xu.sso.db.dao.base.DepartmentMapper;
import com.xu.sso.db.model.base.Department;
import com.xu.sso.db.model.base.DepartmentExample;
import com.xu.sso.db.service.DepartmentDBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DepartmentDBServiceImpl implements DepartmentDBService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public Department findDepartmentByName(String depName) {
        DepartmentExample example = new DepartmentExample();
        example.createCriteria().andDepNameEqualTo(depName);
        List<Department> list = departmentMapper.selectByExample(example);
        if (null != list && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Department findDepartmentById(Integer id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> findAllDepartment() {
        return departmentMapper.selectByExample(new DepartmentExample());
    }

    @Override
    public int createDepartment(Department department) {
        department.setCreateTime(new Date());
        department.setUpdateTime(new Date());
        return departmentMapper.insertSelective(department);
    }

    @Override
    public int updateDepartment(Department department) {
        department.setUpdateTime(new Date());
        return departmentMapper.updateByPrimaryKeySelective(department);
    }

    @Override
    public int deleteById(Integer id) {
        return departmentMapper.deleteByPrimaryKey(id);
    }

}
